<?php

namespace app\controllers\api;

use app\models\Country;
use app\models\Loan;
use app\models\LoanRate;
use app\models\Member;
use app\models\News;
use app\models\Order;
use app\models\Product;
use app\models\Transfer;
use app\models\User;
use app\models\UserProduct;
use Yii;
use yii\base\Exception;
use yii\base\UserException;
use yii\web\NotFoundHttpException;

class CountryController extends BaseController {
    // 国家列表
    public function actionList() {
        $user = $this->check();
        $models = Country::find()->andWhere([
        ])->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    public function actionUserLoan() {
        $user = $this->check([User::ROLE_GOV]);
        $models = Loan::find()
            ->andWhere([
                'country_id' => $user->country_id
            ])
            ->all();
        $this->log('监管 - 贷款查询', '');
        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 用户列表
    public function actionUserProduct() {
        $user = $this->check([User::ROLE_GOV]);
        $models = Product::find()
            ->leftJoin(User::tableName(), 'user.id=product.user_id')
            ->andWhere([
                'country_id' => $user->country_id
            ])
            ->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 用户列表
    public function actionUserUserProduct() {
        $user = $this->check([User::ROLE_GOV]);
        $models = UserProduct::find()
            ->leftJoin(Product::tableName(), 'user_product.product_id=product.id')
            ->leftJoin(User::tableName(), 'user.id=product.user_id')
            ->andWhere([
                'country_id' => $user->country_id
            ])
            ->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 用户列表
    public function actionMemberList($user_id = null) {
        $user = $this->check([User::ROLE_GOV]);
        $models = Member::find()
            ->leftJoin(User::tableName(), 'user.id = member.user_id')
            ->andFilterWhere([
                'country_id' => $user->country_id,
                'user_id' => $user_id,
            ])
            ->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 用户列表
    public function actionOrderList($user_id = null) {
        $user = $this->check([User::ROLE_GOV]);
        $models = Order::find()
            ->leftJoin('user as trader', 'trader.id = trader_id')
            ->leftJoin('user as provider', 'provider.id = provider.id')
            ->andWhere(['or',
                ['trader.country_id'=>$user->country_id],
                ['provider.country_id'=>$user->country_id]
            ])
            ->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 用户列表
    public function actionUserList() {
        $user = $this->check([User::ROLE_GOV]);
        $models = User::find()->andWhere([
            'country_id' => $user->country_id
        ])->all();

        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    // 修改关税
    public function actionTax() {
        $user = $this->check([User::ROLE_GOV]);
        $this->checkModify();
        $user->country->import_duty = Yii::$app->request->post('import_duty');
        $user->country->export_tax = Yii::$app->request->post('export_tax');
        $user->country->save();
        $this->log('政府 - 修改关税', '');

        return $this->renderSuccess();
    }

    // 修改贷款利率
    public function actionRate() {
        $user = $this->check([User::ROLE_GOV]);
        $this->checkModify();
        $user->country->import_duty = Yii::$app->request->post('loan');
        $user->country->export_tax = Yii::$app->request->post('export_tax');

        $this->transaction(function() use($user) {
            LoanRate::deleteAll([
                'country_id'=>$user->country_id,
            ]);
            foreach((array)Yii::$app->request->post('loan', []) as $cycle => $rate) {
                if(empty($rate) || empty($cycle)) continue;
                $model = new LoanRate();
                $model->country_id = $user->country_id;
                $model->cycle = $cycle;
                $model->loan_rate = $rate;
                $model->save();
            }
            $this->log('政府 - 修改贷款利率', '');
        });

        return $this->renderSuccess();
    }
}
